package com.scr1pt.extension

import com.scr1pt.extension.enum.EFileType
import java.util.*

/**
 * 说明: 获取媒体类型
 *
 * 创建者: tum0r
 *
 * 时间: 2022/2/4 15:34
 */
class MIMEExtension {
    companion object {
        /**
         * 文件媒体类型
         */
        private val MIME = LinkedHashMap<EFileType, LinkedHashMap<String, String>>()

        /**
         * 获取文件的媒体类型
         *
         * @param[fileExt] 文件后缀名
         *
         * @return 数组第一位是大类型，包含Binary和Text，数组第二位是MIME
         */
        fun getMIME(fileExt: String): Array<String> {
            val result = Array(2) { "" }
            result[0] = EFileType.Binary.name
            result[1] = "application/octet-stream"
            val ext = fileExt.lowercase(Locale.getDefault())
            for (item in MIME) {
                if (item.value.containsKey(ext)) {
                    result[0] = item.key.name
                    result[1] = item.value[ext].toString()
                    break
                }
            }
            return result
        }

        /**
         * 初始化媒体类型
         */
        private fun initMIME() {
            // region 文本类型
            val textItems = LinkedHashMap<String, String>()
            textItems["323"] = "text/h323"
            textItems["asa"] = "text/asa"
            textItems["asp"] = "text/asp"
            textItems["biz"] = "text/xml"
            textItems["cml"] = "text/xml"
            textItems["css"] = "text/css"
            textItems["dcd"] = "text/xml"
            textItems["dtd"] = "text/xml"
            textItems["ent"] = "text/xml"
            textItems["fo"] = "text/xml"
            textItems["htc"] = "text/x-component"
            textItems["htm"] = "text/html"
            textItems["html"] = "text/html"
            textItems["htt"] = "text/webviewhtml"
            textItems["htx"] = "text/html"
            textItems["jsp"] = "text/html"
            textItems["math"] = "text/xml"
            textItems["mml"] = "text/xml"
            textItems["mtx"] = "text/xml"
            textItems["odc"] = "text/x-ms-odc"
            textItems["plg"] = "text/html"
            textItems["r3t"] = "text/vndrn-realtext3d"
            textItems["rdf"] = "text/xml"
            textItems["rt"] = "text/vndrn-realtext"
            textItems["sol"] = "text/plain"
            textItems["sor"] = "text/plain"
            textItems["spp"] = "text/xml"
            textItems["stm"] = "text/html"
            textItems["svg"] = "text/xml"
            textItems["tld"] = "text/xml"
            textItems["tsd"] = "text/xml"
            textItems["txt"] = "text/plain"
            textItems["uls"] = "text/iuls"
            textItems["vcf"] = "text/x-vcard"
            textItems["vml"] = "text/xml"
            textItems["vxml"] = "text/xml"
            textItems["wml"] = "text/vndwapwml"
            textItems["wsc"] = "text/scriptlet"
            textItems["wsdl"] = "text/xml"
            textItems["xdr"] = "text/xml"
            textItems["xhtml"] = "text/html"
            textItems["xml"] = "text/xml"
            textItems["xq"] = "text/xml"
            textItems["xql"] = "text/xml"
            textItems["xquery"] = "text/xml"
            textItems["xsd"] = "text/xml"
            textItems["xsl"] = "text/xml"
            textItems["xslt"] = "text/xml"
            textItems["js"] = "text/javascript"
            MIME[EFileType.Text] = textItems
            // endregion
            // region 二进制类型
            val binaryItems = LinkedHashMap<String, String>()
            binaryItems["tif"] = "image/tiff"
            binaryItems["001"] = "application/x-001"
            binaryItems["301"] = "application/x-301"
            binaryItems["906"] = "application/x-906"
            binaryItems["907"] = "drawing/907"
            binaryItems["a11"] = "application/x-a11"
            binaryItems["acp"] = "audio/x-mei-aac"
            binaryItems["ai"] = "application/postscript"
            binaryItems["aif"] = "audio/aiff"
            binaryItems["aifc"] = "audio/aiff"
            binaryItems["aiff"] = "audio/aiff"
            binaryItems["anv"] = "application/x-anv"
            binaryItems["asf"] = "video/x-ms-asf"
            binaryItems["asx"] = "video/x-ms-asf"
            binaryItems["au"] = "audio/basic"
            binaryItems["avi"] = "video/avi"
            binaryItems["awf"] = "application/vndadobeworkflow"
            binaryItems["bmp"] = "application/x-bmp"
            binaryItems["bot"] = "application/x-bot"
            binaryItems["c4t"] = "application/x-c4t"
            binaryItems["c90"] = "application/x-c90"
            binaryItems["cal"] = "application/x-cals"
            binaryItems["cat"] = "application/vndms-pkiseccat"
            binaryItems["cdf"] = "application/x-netcdf"
            binaryItems["cdr"] = "application/x-cdr"
            binaryItems["cel"] = "application/x-cel"
            binaryItems["cer"] = "application/x-x509-ca-cert"
            binaryItems["cg4"] = "application/x-g4"
            binaryItems["cgm"] = "application/x-cgm"
            binaryItems["cit"] = "application/x-cit"
            binaryItems["class"] = "java/*"
            binaryItems["cmp"] = "application/x-cmp"
            binaryItems["cmx"] = "application/x-cmx"
            binaryItems["cot"] = "application/x-cot"
            binaryItems["crl"] = "application/pkix-crl"
            binaryItems["crt"] = "application/x-x509-ca-cert"
            binaryItems["csi"] = "application/x-csi"
            binaryItems["cut"] = "application/x-cut"
            binaryItems["dbf"] = "application/x-dbf"
            binaryItems["dbm"] = "application/x-dbm"
            binaryItems["dbx"] = "application/x-dbx"
            binaryItems["dcx"] = "application/x-dcx"
            binaryItems["der"] = "application/x-x509-ca-cert"
            binaryItems["dgn"] = "application/x-dgn"
            binaryItems["dib"] = "application/x-dib"
            binaryItems["dll"] = "application/x-msdownload"
            binaryItems["doc"] = "application/msword"
            binaryItems["dot"] = "application/msword"
            binaryItems["drw"] = "application/x-drw"
            binaryItems["dwf"] = "Model/vnddwf"
            binaryItems["dwg"] = "application/x-dwg"
            binaryItems["dxb"] = "application/x-dxb"
            binaryItems["dxf"] = "application/x-dxf"
            binaryItems["edn"] = "application/vndadobeedn"
            binaryItems["emf"] = "application/x-emf"
            binaryItems["eml"] = "message/rfc822"
            binaryItems["epi"] = "application/x-epi"
            binaryItems["eps"] = "application/postscript"
            binaryItems["etd"] = "application/x-ebx"
            binaryItems["exe"] = "application/x-msdownload"
            binaryItems["fax"] = "image/fax"
            binaryItems["fdf"] = "application/vndfdf"
            binaryItems["fif"] = "application/fractals"
            binaryItems["frm"] = "application/x-frm"
            binaryItems["g4"] = "application/x-g4"
            binaryItems["gbr"] = "application/x-gbr"
            binaryItems["gif"] = "image/gif"
            binaryItems["gl2"] = "application/x-gl2"
            binaryItems["gp4"] = "application/x-gp4"
            binaryItems["hgl"] = "application/x-hgl"
            binaryItems["hmr"] = "application/x-hmr"
            binaryItems["hpg"] = "application/x-hpgl"
            binaryItems["hpl"] = "application/x-hpl"
            binaryItems["hqx"] = "application/mac-binhex40"
            binaryItems["hrf"] = "application/x-hrf"
            binaryItems["hta"] = "application/hta"
            binaryItems["icb"] = "application/x-icb"
            binaryItems["ico"] = "image/x-icon"
            binaryItems["iff"] = "application/x-iff"
            binaryItems["ig4"] = "application/x-g4"
            binaryItems["igs"] = "application/x-igs"
            binaryItems["iii"] = "application/x-iphone"
            binaryItems["img"] = "application/x-img"
            binaryItems["ins"] = "application/x-internet-signup"
            binaryItems["isp"] = "application/x-internet-signup"
            binaryItems["IVF"] = "video/x-ivf"
            binaryItems["java"] = "java/*"
            binaryItems["jfif"] = "image/jpeg"
            binaryItems["jpe"] = "image/jpeg"
            binaryItems["jpeg"] = "image/jpeg"
            binaryItems["jpg"] = "image/jpeg"
            binaryItems["la1"] = "audio/x-liquid-file"
            binaryItems["lar"] = "application/x-laplayer-reg"
            binaryItems["latex"] = "application/x-latex"
            binaryItems["lavs"] = "audio/x-liquid-secure"
            binaryItems["lbm"] = "application/x-lbm"
            binaryItems["lmsff"] = "audio/x-la-lms"
            binaryItems["ls"] = "application/x-javascript"
            binaryItems["ltr"] = "application/x-ltr"
            binaryItems["m1v"] = "video/x-mpeg"
            binaryItems["m2v"] = "video/x-mpeg"
            binaryItems["m3u"] = "audio/mpegurl"
            binaryItems["m4e"] = "video/mpeg4"
            binaryItems["mac"] = "application/x-mac"
            binaryItems["man"] = "application/x-troff-man"
            binaryItems["mdb"] = "application/msaccess"
            binaryItems["mfp"] = "application/x-shockwave-flash"
            binaryItems["mht"] = "message/rfc822"
            binaryItems["mhtml"] = "message/rfc822"
            binaryItems["mi"] = "application/x-mi"
            binaryItems["mid"] = "audio/mid"
            binaryItems["midi"] = "audio/mid"
            binaryItems["mil"] = "application/x-mil"
            binaryItems["mnd"] = "audio/x-musicnet-download"
            binaryItems["mns"] = "audio/x-musicnet-stream"
            binaryItems["mocha"] = "application/x-javascript"
            binaryItems["movie"] = "video/x-sgi-movie"
            binaryItems["mp1"] = "audio/mp1"
            binaryItems["mp2"] = "audio/mp2"
            binaryItems["mp2v"] = "video/mpeg"
            binaryItems["mp3"] = "audio/mp3"
            binaryItems["mp4"] = "video/mp4"
            binaryItems["mpa"] = "video/x-mpg"
            binaryItems["mpd"] = "application/vndms-project"
            binaryItems["mpe"] = "video/x-mpeg"
            binaryItems["mpeg"] = "video/mpg"
            binaryItems["mpg"] = "video/mpg"
            binaryItems["mpga"] = "audio/rn-mpeg"
            binaryItems["mpp"] = "application/vndms-project"
            binaryItems["mps"] = "video/x-mpeg"
            binaryItems["mpt"] = "application/vndms-project"
            binaryItems["mpv"] = "video/mpg"
            binaryItems["mpv2"] = "video/mpeg"
            binaryItems["mpw"] = "application/vndms-project"
            binaryItems["mpx"] = "application/vndms-project"
            binaryItems["mxp"] = "application/x-mmxp"
            binaryItems["net"] = "image/pnetvue"
            binaryItems["nrf"] = "application/x-nrf"
            binaryItems["nws"] = "message/rfc822"
            binaryItems["out"] = "application/x-out"
            binaryItems["p10"] = "application/pkcs10"
            binaryItems["p12"] = "application/x-pkcs12"
            binaryItems["p7b"] = "application/x-pkcs7-certificates"
            binaryItems["p7c"] = "application/pkcs7-mime"
            binaryItems["p7m"] = "application/pkcs7-mime"
            binaryItems["p7r"] = "application/x-pkcs7-certreqresp"
            binaryItems["p7s"] = "application/pkcs7-signature"
            binaryItems["pc5"] = "application/x-pc5"
            binaryItems["pci"] = "application/x-pci"
            binaryItems["pcl"] = "application/x-pcl"
            binaryItems["pcx"] = "application/x-pcx"
            binaryItems["pdf"] = "application/pdf"
            binaryItems["pdx"] = "application/vndadobepdx"
            binaryItems["pfx"] = "application/x-pkcs12"
            binaryItems["pgl"] = "application/x-pgl"
            binaryItems["pic"] = "application/x-pic"
            binaryItems["pko"] = "application/vndms-pkipko"
            binaryItems["pl"] = "application/x-perl"
            binaryItems["pls"] = "audio/scpls"
            binaryItems["plt"] = "application/x-plt"
            binaryItems["png"] = "image/png"
            binaryItems["pot"] = "application/vndms-powerpoint"
            binaryItems["ppa"] = "application/vndms-powerpoint"
            binaryItems["ppm"] = "application/x-ppm"
            binaryItems["pps"] = "application/vndms-powerpoint"
            binaryItems["ppt"] = "application/vndms-powerpoint"
            binaryItems["pr"] = "application/x-pr"
            binaryItems["prf"] = "application/pics-rules"
            binaryItems["prn"] = "application/x-prn"
            binaryItems["prt"] = "application/x-prt"
            binaryItems["ps"] = "application/postscript"
            binaryItems["ptn"] = "application/x-ptn"
            binaryItems["pwz"] = "application/vndms-powerpoint"
            binaryItems["ra"] = "audio/vndrn-realaudio"
            binaryItems["ram"] = "audio/x-pn-realaudio"
            binaryItems["ras"] = "application/x-ras"
            binaryItems["rat"] = "application/rat-file"
            binaryItems["rec"] = "application/vndrn-recording"
            binaryItems["red"] = "application/x-red"
            binaryItems["rgb"] = "application/x-rgb"
            binaryItems["rjs"] = "application/vndrn-realsystem-rjs"
            binaryItems["rjt"] = "application/vndrn-realsystem-rjt"
            binaryItems["rlc"] = "application/x-rlc"
            binaryItems["rle"] = "application/x-rle"
            binaryItems["rm"] = "application/vndrn-realmedia"
            binaryItems["rmf"] = "application/vndadobermf"
            binaryItems["rmi"] = "audio/mid"
            binaryItems["rmj"] = "application/vndrn-realsystem-rmj"
            binaryItems["rmm"] = "audio/x-pn-realaudio"
            binaryItems["rmp"] = "application/vndrn-rn_music_package"
            binaryItems["rms"] = "application/vndrn-realmedia-secure"
            binaryItems["rmvb"] = "application/vndrn-realmedia-vbr"
            binaryItems["rmx"] = "application/vndrn-realsystem-rmx"
            binaryItems["rnx"] = "application/vndrn-realplayer"
            binaryItems["rp"] = "image/vndrn-realpix"
            binaryItems["rpm"] = "audio/x-pn-realaudio-plugin"
            binaryItems["rsml"] = "application/vndrn-rsml"
            binaryItems["rtf"] = "application/msword"
            binaryItems["rv"] = "video/vndrn-realvideo"
            binaryItems["sam"] = "application/x-sam"
            binaryItems["sat"] = "application/x-sat"
            binaryItems["sdp"] = "application/sdp"
            binaryItems["sdw"] = "application/x-sdw"
            binaryItems["sit"] = "application/x-stuffit"
            binaryItems["slb"] = "application/x-slb"
            binaryItems["sld"] = "application/x-sld"
            binaryItems["slk"] = "drawing/x-slk"
            binaryItems["smi"] = "application/smil"
            binaryItems["smil"] = "application/smil"
            binaryItems["smk"] = "application/x-smk"
            binaryItems["snd"] = "audio/basic"
            binaryItems["spc"] = "application/x-pkcs7-certificates"
            binaryItems["spl"] = "application/futuresplash"
            binaryItems["ssm"] = "application/streamingmedia"
            binaryItems["sst"] = "application/vndms-pkicertstore"
            binaryItems["stl"] = "application/vndms-pkistl"
            binaryItems["sty"] = "application/x-sty"
            binaryItems["swf"] = "application/x-shockwave-flash"
            binaryItems["tdf"] = "application/x-tdf"
            binaryItems["tg4"] = "application/x-tg4"
            binaryItems["tga"] = "application/x-tga"
            binaryItems["tiff"] = "image/tiff"
            binaryItems["top"] = "drawing/x-top"
            binaryItems["torrent"] = "application/x-bittorrent"
            binaryItems["uin"] = "application/x-icq"
            binaryItems["vda"] = "application/x-vda"
            binaryItems["vdx"] = "application/vndvisio"
            binaryItems["vpg"] = "application/x-vpeg005"
            binaryItems["vsd"] = "application/vndvisio"
            binaryItems["vss"] = "application/vndvisio"
            binaryItems["vst"] = "application/vndvisio"
            binaryItems["vsw"] = "application/vndvisio"
            binaryItems["vsx"] = "application/vndvisio"
            binaryItems["vtx"] = "application/vndvisio"
            binaryItems["wav"] = "audio/wav"
            binaryItems["wax"] = "audio/x-ms-wax"
            binaryItems["wb1"] = "application/x-wb1"
            binaryItems["wb2"] = "application/x-wb2"
            binaryItems["wb3"] = "application/x-wb3"
            binaryItems["wbmp"] = "image/vndwapwbmp"
            binaryItems["wiz"] = "application/msword"
            binaryItems["wk3"] = "application/x-wk3"
            binaryItems["wk4"] = "application/x-wk4"
            binaryItems["wkq"] = "application/x-wkq"
            binaryItems["wks"] = "application/x-wks"
            binaryItems["wm"] = "video/x-ms-wm"
            binaryItems["wma"] = "audio/x-ms-wma"
            binaryItems["wmd"] = "application/x-ms-wmd"
            binaryItems["wmf"] = "application/x-wmf"
            binaryItems["wmv"] = "video/x-ms-wmv"
            binaryItems["wmx"] = "video/x-ms-wmx"
            binaryItems["wmz"] = "application/x-ms-wmz"
            binaryItems["wp6"] = "application/x-wp6"
            binaryItems["wpd"] = "application/x-wpd"
            binaryItems["wpg"] = "application/x-wpg"
            binaryItems["wpl"] = "application/vndms-wpl"
            binaryItems["wq1"] = "application/x-wq1"
            binaryItems["wr1"] = "application/x-wr1"
            binaryItems["wri"] = "application/x-wri"
            binaryItems["wrk"] = "application/x-wrk"
            binaryItems["ws"] = "application/x-ws"
            binaryItems["ws2"] = "application/x-ws"
            binaryItems["wvx"] = "video/x-ms-wvx"
            binaryItems["xdp"] = "application/vndadobexdp"
            binaryItems["xfd"] = "application/vndadobexfd"
            binaryItems["xfdf"] = "application/vndadobexfdf"
            binaryItems["xls"] = "application/vndms-excel"
            binaryItems["xlw"] = "application/x-xlw"
            binaryItems["xpl"] = "audio/scpls"
            binaryItems["xwd"] = "application/x-xwd"
            binaryItems["x_b"] = "application/x-x_b"
            binaryItems["sis"] = "application/vndsymbianinstall"
            binaryItems["sisx"] = "application/vndsymbianinstall"
            binaryItems["x_t"] = "application/x-x_t"
            binaryItems["ipa"] = "application/vndiphone"
            binaryItems["apk"] = "application/vndandroidpackage-archive"
            binaryItems["xap"] = "application/x-silverlight-app"
            MIME[EFileType.Binary] = binaryItems
            // endregion
        }

        init {
            initMIME()
        }
    }
}